<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="Nuxt进一步学习"><meta name="keywords" content="nuxt B站 axios proxy router"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>Nuxt进一步学习 | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%A9%E5%B1%95%E8%B7%AF%E7%94%B1"><span class="toc-number">1.</span> <span class="toc-text">扩展路由</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E5%AE%88%E5%8D%AB"><span class="toc-number">2.</span> <span class="toc-text">路由守卫</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%89%8D%E7%BD%AE"><span class="toc-number">2.1.</span> <span class="toc-text">前置</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%90%8E%E7%BD%AE"><span class="toc-number">2.2.</span> <span class="toc-text">后置</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E4%BA%A4%E4%BA%92"><span class="toc-number">3.</span> <span class="toc-text">数据交互</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#axios%E6%8B%A6%E6%88%AA%E9%85%8D%E7%BD%AE"><span class="toc-number">3.1.</span> <span class="toc-text">axios拦截配置</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#loading"><span class="toc-number">4.</span> <span class="toc-text">loading</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#vuex"><span class="toc-number">5.</span> <span class="toc-text">vuex</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%8A%B6%E6%80%81%E6%8C%81%E4%B9%85%E5%8C%96%E5%92%8Ctoken%E6%A0%A1%E9%AA%8C"><span class="toc-number">6.</span> <span class="toc-text">状态持久化和token校验</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ui%E7%BB%84%E4%BB%B6%E5%BA%93%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">7.</span> <span class="toc-text">ui组件库的使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plugins%E6%B7%B7%E5%85%A5"><span class="toc-number">8.</span> <span class="toc-text">plugins混入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#meta%E4%BF%A1%E6%81%AF%E6%B3%A8%E5%85%A5"><span class="toc-number">9.</span> <span class="toc-text">meta信息注入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B5%84%E6%BA%90%E8%B7%AF%E5%BE%84"><span class="toc-number">10.</span> <span class="toc-text">资源路径</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#typescript%E6%94%AF%E6%8C%81"><span class="toc-number">11.</span> <span class="toc-text">typescript支持</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">Nuxt进一步学习</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2021-02-06</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">2.6k</span><span class="post-meta__separator">|</span><span>阅读时长: 10 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>主要是对B站一个nuxt的学习视频的简单学习记录，记录一下上一个文章里面有些没有说到的点</p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV13Z4y1T74J?p=5&spm_id_from=pageDriver">B站视频教程</a></p>
<h4 id="扩展路由"><a href="#扩展路由" class="headerlink" title="扩展路由"></a>扩展路由</h4><p>nuxt的路由一般是在pages目录里面约定式的生成的，比如pages里面的index.vue文件就会被当成 <strong>/</strong> 路由匹配的组件，但我们也可以使用扩展路由，即在nuxt.config.js文件的router属性里面对路由进行扩展</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">router: &#123;</span><br><span class="line">    <span class="attr">middleware</span>: <span class="string">&#x27;auth&#x27;</span>,</span><br><span class="line">    <span class="comment">// 扩展路由</span></span><br><span class="line">    extendRoutes (routes, resolve) &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(routes)</span><br><span class="line">      routes.push(&#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&#x27;root&#x27;</span>,</span><br><span class="line">        <span class="attr">path</span>: <span class="string">&#x27;/index&#x27;</span>, <span class="comment">// 这里的路由就是 /index </span></span><br><span class="line">        <span class="attr">component</span>: resolve(__dirname, <span class="string">&#x27;pages/index.vue&#x27;</span>) <span class="comment">// 这里就是pages目录里面的index.vue文件</span></span><br><span class="line">        <span class="comment">// 上面的component引用方式是node的方式，也可使用es的方式 component: ()=&gt;import(&quot;@/pages/index.vue&quot;)</span></span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>



<h4 id="路由守卫"><a href="#路由守卫" class="headerlink" title="路由守卫"></a>路由守卫</h4><h5 id="前置"><a href="#前置" class="headerlink" title="前置"></a>前置</h5><p>依赖于nuxt的中间件</p>
<p>有全局中间件（nuxt.config配置middleware ；layout配置；插件配置）和特定中间件（组件独享守卫 middleware ）</p>
<p>特定中间件</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="title">middleware</span>(<span class="params">context</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;middleware pages&#x27;</span>);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<p>插件的方式</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">router.js</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (&#123; app, redirect &#125;) =&gt; &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;插件前置路由守卫&#x27;</span>)</span><br><span class="line">  <span class="comment">// app是vue的实例</span></span><br><span class="line">  <span class="comment">// redirect是路由跳转函数</span></span><br><span class="line">    </span><br><span class="line">  <span class="comment">// 下面的这两个都是全局的路由守卫</span></span><br><span class="line">  app.router.beforeEach(<span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 全局前置的守卫</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;vue的钩子进行路由守卫&#x27;</span>,to)</span><br><span class="line">    <span class="comment">// 注意这里的跳转只能由redirect进行，不能由next进行</span></span><br><span class="line">    <span class="keyword">if</span> (to.name === <span class="string">&#x27;login&#x27;</span> || to.name === <span class="string">&#x27;reg&#x27;</span>) &#123;</span><br><span class="line">       next()</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">       <span class="comment">// 这里的name属性是路由文件的别名，nuxt里面就是文件名，嵌套路由的话需要带目录名 如 home-set</span></span><br><span class="line">       redirect(&#123; <span class="attr">name</span>: <span class="string">&#x27;login&#x27;</span> &#125;)</span><br><span class="line">       <span class="comment">// 或者 redirect(&#x27;/login&#x27;)</span></span><br><span class="line">    &#125;</span><br><span class="line">    next()</span><br><span class="line">  &#125;)</span><br><span class="line">    </span><br><span class="line">  app.router.afterEach(<span class="function">(<span class="params">to, <span class="keyword">from</span></span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;插件全局后置守卫&#x27;</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 注意插件的方式的话，需要在nuxt.config里面的plugin属性里面引入这个插件文件</span></span><br><span class="line"><span class="attr">plugins</span>: [</span><br><span class="line">    <span class="string">&#x27;~plugins/router.js&#x27;</span>,</span><br><span class="line">  ],</span><br></pre></td></tr></table></figure>

<h5 id="后置"><a href="#后置" class="headerlink" title="后置"></a>后置</h5><p>依赖于vue的beforeRouterLeave钩子（这个东西是在组件内部使用）</p>
<p><a target="_blank" rel="noopener" href="https://router.vuejs.org/zh/guide/advanced/navigation-guards.html#%E8%B7%AF%E7%94%B1%E7%8B%AC%E4%BA%AB%E7%9A%84%E5%AE%88%E5%8D%AB">vue-router官方网址</a></p>
<p>可以在路由组件内直接定义以下路由导航守卫：</p>
<ul>
<li><code>beforeRouteEnter</code></li>
<li><code>beforeRouteUpdate</code> (2.2 新增)</li>
<li><code>beforeRouteLeave</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="title">beforeRouteLeave</span>(<span class="params">to, <span class="keyword">from</span>, next</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> bl = <span class="built_in">window</span>.confirm(<span class="string">&#x27;是否离开&#x27;</span>)</span><br><span class="line">    next(bl)</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>



<h4 id="数据交互"><a href="#数据交互" class="headerlink" title="数据交互"></a>数据交互</h4><p>就是使用axios进行数据请求，有时候可能需要使用proxy代理一下防止跨域</p>
<p><a target="_blank" rel="noopener" href="https://axios.nuxtjs.org/setup">nuxt-axios官网</a></p>
<p>一般直接使用nuxt自己集成的axios模块，有时候可能需要安装一下</p>
<p><strong>@nuxtjs/axios    @nuxtjs/proxy</strong></p>
<p>在nuxt.confgi里面的modules里面添加axios</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">modules: [</span><br><span class="line">	<span class="string">&#x27;@nuxtjs/axios&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;@nuxtjs/proxy&#x27;</span></span><br><span class="line">],</span><br><span class="line"><span class="attr">axios</span>: &#123;</span><br><span class="line">	<span class="attr">proxy</span>: <span class="literal">true</span>, <span class="comment">// 开启跨域行为</span></span><br><span class="line">	<span class="comment">// prefix: &#x27;/api&#x27;, //baseUrl</span></span><br><span class="line">&#125;,</span><br><span class="line"><span class="attr">proxy</span>: &#123; <span class="comment">// 代理</span></span><br><span class="line"><span class="comment">// 注意这里是 /api/ 这是nuxt-axios自己规定的方式，有些跟vue-axios不同</span></span><br><span class="line"><span class="string">&#x27;/api/&#x27;</span>: &#123;</span><br><span class="line">      <span class="attr">target</span>: <span class="string">&#x27;http://example.com&#x27;</span>, <span class="comment">//代理转发地址</span></span><br><span class="line">      <span class="attr">changeOrigin</span>: <span class="literal">true</span>,</span><br><span class="line">      <span class="attr">pathRewrite</span>: &#123;</span><br><span class="line">         <span class="string">&#x27;^/api/&#x27;</span>: <span class="string">&#x27;&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<p>代理后的使用方式</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="title">asyncData</span>(<span class="params">&#123;$axios&#125;</span>)</span>&#123;</span><br><span class="line">    <span class="comment">// 注意使用的时候也要 使用 /api/ </span></span><br><span class="line">	<span class="keyword">const</span> res = <span class="keyword">await</span> $axios.get(<span class="string">&#x27;/api/&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> &#123; data &#125; = <span class="keyword">await</span> $axios.get(<span class="string">&#x27;https://api.chengsong.info&#x27;</span>)</span><br><span class="line">    <span class="comment">// 或者这里直接使用import引入axios进行使用,这里使用了proxy代理</span></span><br><span class="line">    <span class="comment">// const data1 = await axios.get(&#x27;/api/post&#x27;, &#123;</span></span><br><span class="line">    <span class="comment">//   params: &#123; id: 2 &#125;,</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line">    <span class="comment">// const data2 = await axios(&#123;</span></span><br><span class="line">    <span class="comment">//   url: &#x27;/api/post&#x27;,</span></span><br><span class="line">    <span class="comment">//   params: &#123;</span></span><br><span class="line">    <span class="comment">//     id: 1,</span></span><br><span class="line">    <span class="comment">//   &#125;,</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line">    <span class="keyword">return</span> &#123; <span class="attr">res</span>:res,<span class="attr">info1</span>: data, <span class="attr">info2</span>: data1.data &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="axios拦截配置"><a href="#axios拦截配置" class="headerlink" title="axios拦截配置"></a>axios拦截配置</h5><p>这里的话主要是通过插件的方式进行使用</p>
<p>在pugins目录里面创建axios拦截文件，这些具体的api都是nuxt-axios自己有的方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 拦截器</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (&#123; $axios, redirect, route, store &#125;) =&gt; &#123;</span><br><span class="line">  <span class="comment">// 基本配置</span></span><br><span class="line">  $axios.defaults.timeout = <span class="number">10000</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 请求时拦截</span></span><br><span class="line">  $axios.onRequest(<span class="function">(<span class="params">config</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;发送请求时的拦截&#x27;</span>)</span><br><span class="line">    <span class="comment">// config.header.token = &#x27;加token&#x27;</span></span><br><span class="line">    <span class="comment">// config.headers.token = store.state.user.token</span></span><br><span class="line">    <span class="comment">// return config</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 响应拦截</span></span><br><span class="line">  $axios.onResponse(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;响应时的拦截&#x27;</span>)</span><br><span class="line">    <span class="comment">// if (res.data.err === 2 &amp;&amp; route.fullPath !== &#x27;/login&#x27;) &#123;</span></span><br><span class="line">    <span class="comment">//   redirect(&#x27;/login?path=&#x27; + route.fullPath)</span></span><br><span class="line">    <span class="comment">// &#125;</span></span><br><span class="line">    <span class="keyword">return</span> res</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 错误处理</span></span><br><span class="line">  $axios.onError(<span class="function">(<span class="params">error</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;出现错误&#x27;</span>, error)</span><br><span class="line">    <span class="comment">// 处理</span></span><br><span class="line">    <span class="keyword">return</span> error</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>然后在nuxt.config里面的plugins属性里面引入（注意这里的 <strong>~</strong> 在nuxt默认代表根目录，可在 <a target="_blank" rel="noopener" href="https://www.nuxtjs.cn/guide/assets">资源文件</a> 里面看一下使用方式）</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">plugins: [</span><br><span class="line">    <span class="string">&#x27;~plugins/router&#x27;</span>,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">src</span>: <span class="string">&#x27;~plugins/axios&#x27;</span>,</span><br><span class="line">      <span class="attr">ssr</span>: <span class="literal">true</span> <span class="comment">// 开启服务端渲染，不要这个也没啥大事</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br></pre></td></tr></table></figure>

<h4 id="loading"><a href="#loading" class="headerlink" title="loading"></a><a target="_blank" rel="noopener" href="https://www.nuxtjs.cn/api/configuration-loading">loading</a></h4><p>默认情况下，Nuxt.js 使用自己的组件来显示路由跳转之间的进度条。</p>
<p>您可以通过<a target="_blank" rel="noopener" href="https://www.nuxtjs.cn/api/configuration-loading">Configuration 的加载选项</a>全局禁用或自定义它，但也可以通过将 <code>loading</code> 属性设置为 <code>false</code> 来禁用特定页面：</p>
<p>也可以在nuxt.config里面配置loading，可以配置其颜色，也可以配置成一个特定的组件（这个组件也提供了一下方法进行修改）</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 可以设置loading 也可以指定自己的组件</span></span><br><span class="line"><span class="comment">// loading: &#123;</span></span><br><span class="line"><span class="comment">//   color: &#x27;red&#x27;,</span></span><br><span class="line"><span class="comment">//   height: &#x27;3px&#x27;</span></span><br><span class="line"><span class="comment">// &#125;,</span></span><br><span class="line"><span class="attr">loading</span>: <span class="string">&#x27;~components/loading.vue&#x27;</span>,</span><br></pre></td></tr></table></figure>



<h4 id="vuex"><a href="#vuex" class="headerlink" title="vuex"></a><a target="_blank" rel="noopener" href="https://www.nuxtjs.cn/guide/vuex-store">vuex</a></h4><p>其实感觉吧，这个vuex并没有太多的东西可讲，就一个fetch()方法可能是新的，其他的感觉跟vue-vuex基本没啥大的区别，就简单写一些感觉新的东西吧</p>
<p>1.使用模块的方式 store目录里面的每个js文件都会转换成状态树，index是主模块，其他的就在modules里面</p>
<p><strong>state</strong>必须暴露为一个函数，其他的可以是一个对象</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// index.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> state = <span class="function">() =&gt;</span> (&#123;</span><br><span class="line">  <span class="attr">counter</span>: <span class="number">0</span>,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> mutations = &#123;</span><br><span class="line">  <span class="function"><span class="title">increment</span>(<span class="params">state</span>)</span> &#123;</span><br><span class="line">    state.counter++</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// todos.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> state = <span class="function">() =&gt;</span> (&#123;</span><br><span class="line">  <span class="attr">list</span>: [<span class="string">&#x27;默认&#x27;</span>],</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> mutations = &#123;</span><br><span class="line">  <span class="function"><span class="title">add</span>(<span class="params">state, text</span>)</span> &#123;</span><br><span class="line">    state.list.push(&#123;</span><br><span class="line">      text,</span><br><span class="line">      <span class="attr">done</span>: <span class="literal">false</span>,</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">remove</span>(<span class="params">state, &#123; todo &#125;</span>)</span> &#123;</span><br><span class="line">    state.list.splice(state.list.indexOf(todo), <span class="number">1</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 外部使用</span></span><br><span class="line"><span class="built_in">this</span>.info = <span class="built_in">this</span>.$store.state.todos.list</span><br><span class="line"><span class="comment">// 使用子模块的方法</span></span><br><span class="line"><span class="built_in">this</span>.$store.commit(<span class="string">&#x27;todos/add&#x27;</span>, e.target.value)</span><br></pre></td></tr></table></figure>



<h4 id="状态持久化和token校验"><a href="#状态持久化和token校验" class="headerlink" title="状态持久化和token校验"></a>状态持久化和token校验</h4><p>持久化的话，如果使用cookie，需要安装一个包<strong>cookie-universal-nuxt</strong> ，并在nuxt.config的modules里面引入</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">modules: [</span><br><span class="line">        &#x27;cookie-universal-nuxt&#x27;,</span><br><span class="line">],</span><br></pre></td></tr></table></figure>

<p>然后使用的方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> key = <span class="string">&quot;love_home_token&quot;</span></span><br><span class="line"><span class="comment">// 这里的app是vue实例</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">setToken</span>(<span class="params">app, token</span>) </span>&#123;</span><br><span class="line">    app.$cookies.set(key, token);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">getToken</span>(<span class="params">app</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> app.$cookies.get(key);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>token验证的话</p>
<p>一般登录时同步vuex和cookie，如果用户刷新了，nuxtServerInit钩子会被调用，取出cookie，同步vuex，axios拦截器读取vuex（因为vuex存在内存中，所以速度会比存在磁盘的cookie快），设置token</p>
<h4 id="ui组件库的使用"><a href="#ui组件库的使用" class="headerlink" title="ui组件库的使用"></a><a target="_blank" rel="noopener" href="https://www.cnblogs.com/jofun/p/13234973.html">ui组件库的使用</a></h4><p>这里以element-ui为例，其实目前nuxt在创建项目的时候就会让你选择一个UI组件库，如果在创建的时候选了，那就直接用就是了，如果没有的话，就使用npm安装一下，在plugins目录里面创建element.js文件（这里是全局引入）</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Element <span class="keyword">from</span> <span class="string">&#x27;element-ui&#x27;</span></span><br><span class="line"><span class="keyword">import</span> locale <span class="keyword">from</span> <span class="string">&#x27;element-ui/lib/locale/lang/en&#x27;</span></span><br><span class="line"></span><br><span class="line">Vue.use(Element, &#123; locale &#125;)</span><br></pre></td></tr></table></figure>

<p>然后在nuxt.config里面的plugins里面引入这个文件并在css里面引入element的css文件</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">css: [</span><br><span class="line">     <span class="string">&#x27;element-ui/lib/theme-chalk/index.css&#x27;</span>  <span class="comment">// 引入全局样式</span></span><br><span class="line">],</span><br><span class="line"><span class="attr">plugins</span>: [</span><br><span class="line"> 	&#123;<span class="attr">src</span>: <span class="string">&#x27;~plugins/element&#x27;</span>, <span class="attr">ssr</span>: <span class="literal">true</span> &#125; <span class="comment">// ssr:true代表在服务端渲染,客户端不渲染</span></span><br><span class="line">],</span><br></pre></td></tr></table></figure>

<p>如果需要按需引入的话，需要先安装一个插件 <strong>babel-plugin-component</strong> 然后在上面的element.js文件里面按需引入</p>
<p>同时如果需要在客户端的时候不引入element，则需要对build进行配置</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">// nuxt.config.js</span><br><span class="line">build: &#123;</span><br><span class="line">    transpile: [/^element-ui/],</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>



<h4 id="plugins混入"><a href="#plugins混入" class="headerlink" title="plugins混入"></a>plugins混入</h4><p>就是提一下这个用处，当在对vue实例进行插入的时候，既然我们都拿到Vue实例了，那所有的vue自己的关于全局的东西都能被使用，如全局过滤器（Vue.filter），全局指令（Vue.directive），全局组件（Vue.component），混入mixin（Vue.mixin）这些都能被使用</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 全局方法</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">let</span> show = <span class="function">() =&gt;</span> <span class="built_in">console</span>.log(<span class="string">&#x27;全局方法&#x27;</span>)</span><br><span class="line"></span><br><span class="line">Vue.prototype.$show = show <span class="comment">// 服务端钩子内部不可以使用  服务端this不会指向vue实例</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 全局过滤器</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> filters <span class="keyword">from</span> <span class="string">&#x27;../assets/js/filter.js&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.keys(filters).forEach(<span class="function"><span class="params">key</span>=&gt;</span>Vue.filter(key, filters[key]))</span><br><span class="line"></span><br><span class="line"><span class="comment">// 全局指令</span></span><br><span class="line"><span class="keyword">import</span> direc1 <span class="keyword">from</span> <span class="string">&#x27;../assets/js/directives/directive1.js&#x27;</span></span><br><span class="line"><span class="keyword">import</span> direc2 <span class="keyword">from</span> <span class="string">&#x27;../assets/js/directives/directive2.js&#x27;</span></span><br><span class="line">Vue.directive(<span class="string">&#x27;direc1&#x27;</span>, direc1)</span><br><span class="line">Vue.directive(<span class="string">&#x27;direc2&#x27;</span>, direc2)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 全局组件</span></span><br><span class="line"><span class="keyword">import</span> UcButton <span class="keyword">from</span> <span class="string">&#x27;../components/global/uc-button.vue&#x27;</span></span><br><span class="line">Vue.component(<span class="string">&#x27;uc-button&#x27;</span>, UcButton)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 混入methods</span></span><br><span class="line">Vue.mixin(&#123; <span class="comment">// 都会有这个</span></span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    $seo(title, content, payload=[]) &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        title,</span><br><span class="line">        <span class="attr">meta</span>: [&#123;</span><br><span class="line">          <span class="attr">hid</span>: <span class="string">&#x27;descript&#x27;</span>,</span><br><span class="line">          <span class="attr">name</span>: <span class="string">&#x27;keywords&#x27;</span>,</span><br><span class="line">          content</span><br><span class="line">        &#125;].concat(payload)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="meta信息注入"><a href="#meta信息注入" class="headerlink" title="meta信息注入"></a><a target="_blank" rel="noopener" href="https://www.nuxtjs.cn/api/configuration-head">meta信息注入</a></h4><p>meta简单的说就是一个关于这个网站的主要信息的总结，是说这个网站是在干什么的</p>
<p>为了爬虫爬取到更多的网页的信息，需要自己定义一下meta里面的内容</p>
<p>实际上nuxtjs是使用了<a target="_blank" rel="noopener" href="https://github.com/nuxt/vue-meta"><code>vue-meta</code></a> <em>更新应用的</em> <code>头部标签(Head)</code> <em>和</em> <code>html 属性</code></p>
<p>对于head可以在nuxt.config里面进行统一配置，也可以在一个单独的页面进行特定的配置</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 组件内部方式</span></span><br><span class="line"><span class="function"><span class="title">head</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">title</span>: <span class="string">&#x27;晟松&#x27;</span>,</span><br><span class="line">      <span class="attr">meta</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">          <span class="attr">hid</span>: <span class="string">&#x27;cs&#x27;</span>,</span><br><span class="line">          <span class="attr">name</span>: <span class="string">&#x27;晟松&#x27;</span>,</span><br><span class="line">          <span class="attr">content</span>: <span class="built_in">this</span>.title,</span><br><span class="line">        &#125;,</span><br><span class="line">      ],</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">      </span><br><span class="line"><span class="comment">// nuxt.config</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  <span class="attr">head</span>: &#123;</span><br><span class="line">    <span class="attr">titleTemplate</span>: <span class="string">&#x27;同一标题&#x27;</span>,</span><br><span class="line">    <span class="attr">meta</span>: [</span><br><span class="line">      &#123; <span class="attr">charset</span>: <span class="string">&#x27;utf-8&#x27;</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">name</span>: <span class="string">&#x27;viewport&#x27;</span>, <span class="attr">content</span>: <span class="string">&#x27;width=device-width, initial-scale=1&#x27;</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">hid</span>: <span class="string">&#x27;description&#x27;</span>, <span class="attr">name</span>: <span class="string">&#x27;description&#x27;</span>, <span class="attr">content</span>: <span class="string">&#x27;Meta description&#x27;</span> &#125;</span><br><span class="line">    ],</span><br><span class="line">    <span class="comment">// 还可以在这里引入一些js脚本，字体文件之类的</span></span><br><span class="line">      <span class="attr">link</span>: [</span><br><span class="line">      &#123; <span class="attr">rel</span>: <span class="string">&#x27;icon&#x27;</span>, <span class="attr">type</span>: <span class="string">&#x27;image/x-icon&#x27;</span>, <span class="attr">href</span>: <span class="string">&#x27;/favicon.ico&#x27;</span> &#125;</span><br><span class="line">    ],</span><br><span class="line">    <span class="attr">script</span>: [</span><br><span class="line">      &#123;<span class="attr">src</span>: <span class="string">&#x27;外部链接&#x27;</span>&#125;</span><br><span class="line">    ],</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="资源路径"><a href="#资源路径" class="headerlink" title="资源路径"></a>资源路径</h4><p>主要是有时候比如 img 或者其他的什么东西，可能需要找到本地的一个资源，这个时候就需要对路径进行指定</p>
<p>相对路径 找到一些需要压缩资源 assets目录 &lt;img src=”../assets/xxx.png” alt=””&gt;</p>
<p>绝对路径 找到无需压缩的资源 static目录 &lt;img src=”/static/bg.jpg” alt=””&gt;    注意这里使用 <strong>/</strong> 开头</p>
<p>~assets ~代表根</p>
<p>@代表 src</p>
<h4 id="typescript支持"><a href="#typescript支持" class="headerlink" title="typescript支持"></a><a target="_blank" rel="noopener" href="https://typescript.nuxtjs.org/zh-Hant/guide/introduction">typescript支持</a></h4><p>可以在使用nuxt项目创建的时候就选择使用typescript，也可以其他方式</p>
<p>先安装 <strong>@nuxt/typescript-build</strong> 然后在nuxt.config里面的引入</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">buildModules</span>: [<span class="string">&#x27;@nuxt/typescript-build&#x27;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>之后再建立**<code>tsconfig.json</code>** 文件</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;compilerOptions&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;target&quot;</span>: <span class="string">&quot;ES2018&quot;</span>,</span><br><span class="line">    <span class="string">&quot;module&quot;</span>: <span class="string">&quot;ESNext&quot;</span>,</span><br><span class="line">    <span class="string">&quot;moduleResolution&quot;</span>: <span class="string">&quot;Node&quot;</span>,</span><br><span class="line">    <span class="comment">// 下面的skip.. 是在一个项目里面新使用ts的时候才需要配置的</span></span><br><span class="line">     <span class="string">&quot;skipLibCheck&quot;</span>:<span class="literal">true</span>,</span><br><span class="line">    <span class="string">&quot;lib&quot;</span>: [</span><br><span class="line">      <span class="string">&quot;ESNext&quot;</span>,</span><br><span class="line">      <span class="string">&quot;ESNext.AsyncIterable&quot;</span>,</span><br><span class="line">      <span class="string">&quot;DOM&quot;</span></span><br><span class="line">    ],</span><br><span class="line">    ....</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>后面还需要创建一个文件，具体的就到官网上去看吧</p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2021/02/06/Nuxt进一步学习/">https://chengsong.info/2021/02/06/Nuxt进一步学习/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/nuxt-B%E7%AB%99-axios-proxy-router/">nuxt B站 axios proxy router</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2021/02/15/vue3%E8%A7%86%E9%A2%91%E7%AE%80%E5%8D%95%E8%AE%B0%E5%BD%95/"><i class="fa fa-chevron-left">  </i><span>vue3视频简单记录</span></a></div><div class="next-post pull-right"><a href="/2021/02/05/Nodejs-express-koa2%E5%88%9D%E5%AD%A6%E4%B9%A0/"><span>Nodejs express koa2初学习</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>